package cn.tradewin.advisor.pages.project.plan;

import java.util.List;

import org.apache.tapestry5.ValueEncoder;
import org.apache.tapestry5.annotations.Environmental;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.SessionState;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.ioc.internal.util.CollectionFactory;
import org.apache.tapestry5.services.javascript.JavaScriptSupport;
import org.slf4j.Logger;

import cn.tradewin.advisor.commons.util.CalendarUtil;
import cn.tradewin.advisor.persist.model.Account;
import cn.tradewin.advisor.persist.model.Project;
import cn.tradewin.advisor.persist.model.ProjectResource;
import cn.tradewin.advisor.persist.services.AccountService;
import cn.tradewin.advisor.persist.services.ProjectService;

public class Resource {
	
	@SessionState
	private Project target;
	
	@Property
	private final AccountEncoder accountEncoder = new AccountEncoder();
	
	@Property
	private List<Account> availabledAccounts;
	
	@Property
	private Account availabledAccount;
	
	@Property
	private List<Account> assignedAccounts;
	
	@Property
	private Account assignedAccount;

	private List<String> selectedAvailabledAccounts;
	
	private List<String> selectedAssignedAccounts;

	@Inject
	private AccountService accountService;
	
	@Inject
	private ProjectService projectService;

	@Environmental
	private JavaScriptSupport jss;
	
	@Inject
	private Logger log;
	
	private boolean inFormSubmission;

	public void onActivate() {
		inFormSubmission = false;
	}

	void onPrepareForSubmit() {
		inFormSubmission = true;
		selectedAvailabledAccounts = CollectionFactory.newList();
		selectedAssignedAccounts = CollectionFactory.newList();
	}
	
	void afterRender() {
		jss.require("submitproxy").with("add","padd");
		jss.require("submitproxy").with("remove","premove");
	}

	void onPrepareForRender() {
		availabledAccounts = accountService.getAccountsNotInProject(target.getId());
		assignedAccounts = accountService.getAccountsInProject(target.getId());
	}

	public boolean isAvlSelected() {
		return false;
	}

	public void setAvlSelected(boolean selected) {
		if (inFormSubmission) {
			if (selected) {
				selectedAvailabledAccounts.add(availabledAccount.getId());
			}
		}
	}
	
	public boolean isAsnSelected() {
		return false;
	}

	public void setAsnSelected(boolean selected) {
		if (inFormSubmission) {
			if (selected) {
				selectedAssignedAccounts.add(assignedAccount.getId());
			}
		}
	}

	
	void onSubmitFromAdd() {
		List<ProjectResource> resources = CollectionFactory.newList();
		ProjectResource pr = null;
		for (String accountId : selectedAvailabledAccounts) {
			 pr = new ProjectResource();
			 pr.setAccountId(accountId);
			 pr.setProjectId(target.getId());
			 pr.setStart(CalendarUtil.format(target.getStart(),null));
			 pr.setEnd(CalendarUtil.format(target.getEnd(), null));
			 pr.setRate(Float.valueOf(1));
			 resources.add(pr);
		}
		projectService.addProjectAccounts(resources);
	}
	
	void onSubmitFromRemove() {
		List<ProjectResource> resources = CollectionFactory.newList();
		ProjectResource pr = null;
		for (String accountId : selectedAssignedAccounts) {
			 pr = new ProjectResource();
			 pr.setAccountId(accountId);
			 pr.setProjectId(target.getId());
			 resources.add(pr);
		}
		projectService.removeProjectAccounts(resources);
	}
		
	private class AccountEncoder implements ValueEncoder<Account> {
		public String toClient(Account value) {
			return value.getId().toString();
		}
		public Account toValue(String clientValue) {
			Account m = new Account();
			m.setId(clientValue);
			return m;
		}
	}	
}
